package ace.cmp.converter.spring.core;

import ace.cmp.converter.api.AceConverter;
import lombok.SneakyThrows;
import org.springframework.beans.BeanUtils;

/**
 * @author caspar
 * @date 2023/9/11 17:37
 */
public class BeanUtilsConverterImpl<S, T> implements AceConverter<S, T> {
  private final Class<S> sClass;
  private final Class<T> tClass;

  public BeanUtilsConverterImpl(Class<S> sClass, Class<T> tClass) {
    this.sClass = sClass;
    this.tClass = tClass;
  }

  @SneakyThrows
  @Override
  public T convert(S source) {
    if (source == null) {
      return null;
    }
    T dest = tClass.newInstance();
    BeanUtils.copyProperties(source, dest);
    return dest;
  }
}
